<?php

namespace Weapp\Shop;

use Weapp\Basic\Basic;
use Weapp\Basic\MyCurlFile;

/**
 * 接入商品前必需接口
 * Class Common
 * @url https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/business-capabilities/ministore/minishopopencomponent2/API/cat/get_children_cateogry.html
 * @package Weapp/Shop
 */
class Common extends Basic
{
    /**
     * 获取商品类目
     * 获取所有三级类目及其资质相关信息 注意：该接口拉到的是【全量】三级类目数据，数据回包大小约为2MB。 所以请商家自己做好缓存，不要频繁调用（有严格的频率限制），该类目数据不会频率变动，推荐商家每天调用一次更新商家自身缓存
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function cat($data = [])
    {
        $url = 'shop/cat/get?t=1';
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 上传图片
     * 上传类目、品牌、商品时，需使用图片上传接口换取临时链接用于上传，上传后微信侧会转为永久链接。临时链接在微信侧永久有效，商家侧可以存储临时链接，避免重复换取链接
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function uploadImg($data)
    {
        $url = 'shop/img/upload';
        if (isset($data['img_url'])) {
            $myFile = new MyCurlFile($data['img_url']);
            $data['media'] = $myFile->get();
        }
        return $this->callPostApi($url, $data, false);
    }

    /**
     * 品牌审核,上传品牌信息
     * 请求成功后将会创建一个审核单，单号将在回包中给出
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function audit_brand($data)
    {
        $url = 'shop/audit/audit_brand';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 上传类目资质
     * 请求成功后将会创建一个审核单，单号将在回包中给出
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function audit_category($data)
    {
        $url = 'shop/audit/audit_category';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }

    /**
     * 获取审核结果
     * 根据审核id，查询品牌和类目的审核结果
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function audit_result($data)
    {
        $url = 'shop/audit/result';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }
    /**
     * 获取小程序资质
     * 获取曾经提交的小程序审核资质 请求类目会返回多次的请求记录，请求品牌只会返回最后一次的提交记录
     * @param array $data
     * @return array
     * @throws \Weapp\Exceptions\InvalidResponseException
     * @throws \Weapp\Exceptions\LocalCacheException
     */
    public function get_miniapp_certificate($data)
    {
        $url = 'shop/audit/get_miniapp_certificate';
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->callPostApi($url, $data, true);
    }
}
